//enter键快捷登录
document.onkeydown = function(event) {
	var e = event || window.event || arguments.callee.caller.arguments[0];
	if(e && e.keyCode == 13) {
		myselect();
	}
};
/*地图定位*/
mapObj = new AMap.Map('mapID');
mapObj.plugin('AMap.Geolocation', function() {
	geolocation = new AMap.Geolocation({
		enableHighAccuracy: true, //是否使用高精度定位，默认:true
		timeout: 10000 //超过10秒后停止定位，默认：无穷大
	});
	geolocation.getCurrentPosition(function(status, result) { //定位
		if(status == "complete") {
			//alert(result.addressComponent.province)
			$("#currCityName span").html(result.addressComponent.province);
		} else {
			$("#currCityName span").html("定位失败");
		}
	});
	//AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位完成信息
	//AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
$(function() {
	load("北京");
	StyleMatch();
});

/*手机样式适配函数*/
function StyleMatch() {
	var _h = window.innerHeight;
	if(_h < 510) { //5S手机高度：h=504
		$("#swipercontainer").addClass("item_5_1");
	} else if(_h < 530) { //7p手机高度：h=529
		$("#swipercontainer").addClass("item_6_1");
	} else if(_h < 540) {
		$("#swipercontainer").addClass("item_7_1");
	} else if(_h < 580) {
		$("#swipercontainer").addClass("item_8_1");
	} else if(_h < 670) {
		$("#swipercontainer").addClass("item_8_1");
	} else {
		$("#swipercontainer").addClass("item_8_1");
	}
	$("body").on("touchmove", function(event) {
		event.preventDefault;
	}, false);
}
/*加载函数*/
function load(text) {
	var loadIndex = layer.open({
		type: 2,
		content: '加载中'
	});
	var postData = {
		city: text
	};
	$.ajax({
		type: "get",
		url: "https://www.baidu.com/home/other/data/weatherInfo",
		data: postData,
		success: function(data) {
			data=JSON.parse(data);
			console.log(data);
			if(data.errNo=="0") {
				if(data.data.weather.content) {
					var obj = data.data.weather.content;
					/*加载上部分的数据*/
					$("#one").html(obj.cityname);
					if(obj.warning){
						$("#dayweather").html(obj.warning.title).css("color",obj.warning.color);
					}else{
						$("#dayweather").html("");
					}
					$("#wind").html("更新于："+obj.week);
					$("#date").html("数据来源："+obj.source.name);
					/*下面的轮播图数据*/
					var casts = [];
					casts.push(obj.today);
					casts.push(obj.tomorrow);
					casts.push(obj.thirdday);
					casts.push(obj.fourthday);
					casts.push(obj.fifthday);
					var resultStr = "";
					$.each(casts, function(i, item) {
						resultStr += "<div class=\"swiper-slide\">";
						resultStr += "	<section>";
						resultStr += "		<p>"+item.date + "  " + item.time.slice(0,2) + "</p>";
						resultStr += "		<img width=\"50\" height=\"50\" src=\"" + item.img[0] + "\"/img>";
						resultStr += "		<p>" + item.temp + "</p>";
						resultStr += "		<p>" + item.condition + "</p>";
						resultStr += "		<p>" + item.wind + "</p>";
						resultStr += "	</section>";
						resultStr += "</div>";
					});
					$("#swiperHtml").html(resultStr);
					var swiper = new Swiper('.swiper-container', {
						pagination: '.swiper-pagination',
						slidesPerView: 2,
						spaceBetween: 0
					});
					$(".mylayer").hide();
					$('.downdiv').removeClass("updiv");
				} else {
					layer.open({
						content: '未查询到该城市信息，请重新输入！',
						btn: '确定'
					});
				}
			} else {
				layer.open({
					content: '未查询到相关城市信息，请重新输入!',
					btn: '确定'
				});
			}
			layer.close(loadIndex);
		}
	});
}

/*点击遮罩层函数*/
function mylayer() {
	$(".mylayer").hide();
	$('.downdiv').removeClass("updiv");
}
/*右上角图标点击函数*/
function myshow() {
	$(".mylayer").show();
	$('.downdiv').addClass("updiv");
	$(".downdiv").show();
}
/*点击查询函数*/
function myselect() {
	var cityName = $("#cityName").val();
	if(cityName == "") {
		layer.open({
			content: '请输入城市名称！',
			btn: '确定'
		});
		return;
	} else {
		load(cityName);
	}
}